package com;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapCount {
    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        String str = "123,456,789,123,456";
        //把给定字符串分割开
        String[] temp = str.split(",");
        //遍历字符串数组，放入map中
        for (String s:temp) {
            int cnt=1;
            //如果map之前就有该字符串，就把原来的value值+1
            if(map.containsKey(s)){
                cnt+=map.get(s);
            }
            map.put(s,cnt);
        }
        //Map转化为Set,便于遍历输出Map的key-value
        Set<String> set = map.keySet();
        for (String k:set) {
            System.out.println(k+"出现了"+map.get(k));
        }
    }
}
